<?php /* COMPANIES $Id: addedit.php,v 1.49.4.2 2005/11/07 04:44:49 pedroix Exp $ */
$company_id = intval( dPgetParam( $_GET, 'company_id', 0 ) );

// check permissions for this company
$perms =& $AppUI->acl();
// If the company exists we need edit permission,
// If it is a new company we need add permission on the module.
if ($company_id)
	$canEdit = $perms->checkModuleItem($m, 'edit', $company_id);
else
	$canEdit = $perms->checkModule($m, 'add');

if (!$canEdit) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}

// load the company types
$types = dPgetSysVal( 'CompanyType' );

// load the record data
$q  = new DBQuery;
$q->addTable('companies');
$q->addQuery('companies.*');
$q->addQuery('con.contact_first_name');
$q->addQuery('con.contact_last_name');
$q->addJoin('users', 'u', 'u.user_id = companies.company_owner');
$q->addJoin('contacts', 'con', 'u.user_contact = con.contact_id');
$q->addWhere('companies.company_id = '.$company_id);
$sql = $q->prepare();
$q->clear();

$obj = null;
if (!db_loadObject( $sql, $obj ) && $company_id > 0) {
	// $AppUI->setMsg( '	$qid =& $q->exec(); Company' ); // What is this for?
	$AppUI->setMsg( 'invalidID', UI_MSG_ERROR, true );
	$AppUI->redirect();
}

// collect all the users for the company owner list
$q  = new DBQuery;
$q->addTable('users','u');
$q->addTable('contacts','con');
$q->addQuery('user_id');
$q->addQuery('CONCAT_WS(" ",contact_first_name,contact_last_name)'); 
$q->addOrder('contact_first_name');
$q->addWhere('u.user_contact = con.contact_id');
$q->addWhere('u.user_id <> 1');
$owners = $q->loadHashList();

// setup the title block
$ttl = $company_id > 0 ? 'Edit Company' : 'Add Company';
$titleBlock = new CTitleBlock( $ttl );
$titleBlock->addButton( 'company list', 'index.php?m=companies' );
$titleBlock->show();
?>

<script language="javascript">
function submitIt() {
	var form = document.changeclient;
	if (form.company_name.value.length < 3) {
		alert( '<?=$AppUI->_('companyValidName', UI_OUTPUT_JS)?>' );
		form.company_name.focus();
	} else {
		form.submit();
	}
}

function testURL( x ) {
	var test = document.changeclient.company_primary_url.value;
	if (test.length > 6) {
		newwin = window.open( 'http://' + test, 'newwin', '' );
	}
}
</script>
<form name="changeclient" action="?m=companies" method="post">
	<input type="hidden" name="dosql" value="do_company_aed" />
	<input type="hidden" name="company_id" value="<?=$company_id?>" />
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr><td class="tabon">Information</td><td width="100%"></td></tr>
</table>
<table border="0" cellpadding="7" cellspacing="0" width="100%" class="infopanel">
<tr>
	<td><input type="button" value="<?=$AppUI->_('Save')?>" class="button" onClick="submitIt()"/></td>
	<td><input type="button" value="<?=$AppUI->_('Cancel')?>" class="button" onClick="history.back(-1)"/></td>
	<td width="100%">&nbsp;</td>
<? if ($company_id != 0) { ?>
	<td><?=showButton($AppUI->_('View'), '?m=companies&a=view&company_id='.$company_id)?></td>
<? } ?>
<tr>
</table>
<table cellspacing="0" cellpadding="7" border="0" width='100%' class="subinfopanel">
<tr>
<td>
	<table>
	<tr>
		<td align="right">* <?=$AppUI->_('Company Name')?></td>
		<td colspan="3">
			<input type="text" class="text" name="company_name" value="<?=dPformSafe(@$obj->company_name);?>"
				style="width: 275px" maxlength="255"/>
		</td>
		<td align="right"><?=$AppUI->_('Type')?></td>
		<td>
	<?php
		echo arraySelect( $types, 'company_type', 'size="1" class="text"', @$obj->company_type, true );
	?>
		</td>
	</tr>
	<tr>
		<td align="right">Website http://<A name="x"></a></td>
		<td colspan="3">
			<input type="text" class="text" name="company_primary_url" value="<?=dPformSafe(@$obj->company_primary_url)?>"
				style="width: 275px" maxlength="255"/>
		</td>
		<td>
			<a href="#x" onClick="testURL('CompanyURLOne')">[<?=$AppUI->_('test')?>]</a>
		</td>
	</tr>
	<tr>
		<td align="right"><?=$AppUI->_('Email')?></td>
		<td colspan="3">
			<input type="text" class="text" name="company_email" value="<?=dPformSafe(@$obj->company_email)?>"
				style="width: 275px" maxlength="255"/>
		</td>
	</tr>
	<tr>
		<td align="right"><?=$AppUI->_('Phone')?></td>
		<td>
			<input type="text" class="text" name="company_phone1" value="<?=dPformSafe(@$obj->company_phone1)?>"
				style="width: 110px" maxlength="30"/>
		</td>
		<td align="center"><?=$AppUI->_('or')?></td>
		<td>
			<input type="text" class="text" name="company_phone2" value="<?=dPformSafe(@$obj->company_phone2)?>"
				style="width: 110px" maxlength="30"/>
		</td>
		<td align="right"><?=$AppUI->_('Fax')?></td>
		<td>
			<input type="text" class="text" name="company_fax" value="<?=dPformSafe(@$obj->company_fax)?>"
				style="width: 100px" maxlength="30"/>
		</td>
	</tr>
	<tr>
		<td align="right" valign="top"><?=$AppUI->_('Address')?></td>
		<td colspan="5">
			<textarea class="text" style="width: 420px" maxlength="255" rows="2" name="company_address1"><?=dPformSafe(@$obj->company_address1)?></textarea>
		</td>
	</tr>
	<tr style="display: none">
		<td colspan="6"><input type="hidden" name="company_address2" value="<?=dPformSafe(@$obj->company_address2)?>" size=50 maxlength="255" /></td>
	</tr>
	<tr>
		<td align="right"><?=$AppUI->_('City')?></td>
		<td>
			<input type="text" class="text" name="company_city" value="<?=dPformSafe(@$obj->company_city)?>"
				style="width: 110px" maxlength="50" /></td>
		<td align="right"><?=$AppUI->_('State')?></td>
		<td><input type="text" class="text" name="company_state" value="<?=dPformSafe(@$obj->company_state)?>"
				style="width: 110px" maxlength="50" /></td>
		<td align="right"><?=$AppUI->_('Zip')?></td>
		<td><input type="text" class="text" name="company_zip" value="<?=dPformSafe(@$obj->company_zip)?>"
				style="width: 100px" maxlength="15"/></td>
	</tr>
<? if ($AppUI->user_id == 1) { ?>
	<tr>
		<td align="right"><?=$AppUI->_('Company Owner')?></td>
		<td>
	<?php
		echo arraySelect( $owners, 'company_owner', 'size="1" class="text"', @$obj->company_owner ? $obj->company_owner : $AppUI->user_id );
	?>
		</td>
	</tr>
<? } else { ?>
	<input type="hidden" name="company_owner" value="<?=@$obj->company_owner ? $obj->company_owner : $AppUI->user_id?>"/>
<? } ?>
	<tr>
		<td align="right" valign=top><?=$AppUI->_('Description')?></td>
		<td colspan="5" align="left">
		<textarea style="width: 420px" rows="6" class="textarea" name="company_description"><?=@$obj->company_description?></textarea>
		</td>
	</tr>
</table>
</td>
	<td align='left'>
		<?php
 			require_once('./classes/CustomFields.class.php');
 			$custom_fields = new CustomFields( $m, $a, $obj->company_id, 'edit' );
 			$custom_fields->printHTML();
		?>		
	</td>
</tr>
</table>
</form>
